package com.gongJiao.controller;


import com.gongJiao.Vo.*;
import com.gongJiao.common.Result;
import com.gongJiao.entity.Route;
import com.gongJiao.entity.Type;
import com.gongJiao.entity.Vehicle;
import com.gongJiao.service.RouteService;
import com.gongJiao.service.TypeService;
import com.gongJiao.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author ${author}
 * @since 2021-12-25
 */
@Controller
@RequestMapping("/vehicle")
public class VehicleController {
    @Autowired
    VehicleService vehicleService;
    @Autowired
    RouteService routeService;
    @Autowired
    TypeService typeService;

    /**
     * 后台分页查询公交信息列表
     * @return
     */
    @GetMapping("getVehiclePageList")
    @ResponseBody
    public PageResult<AdminVehicleVo> getVehiclePageList( VehicleVo vehicleVo){
        PageResult<AdminVehicleVo> pageResult=new PageResult<AdminVehicleVo>();
        if(vehicleVo.getStartSite()==null && vehicleVo.getEndSite()==null){
            //根据名字查询以及正常查询
            //查询所以数据
            List<AdminVehicleVo> list=vehicleService.getVehicleList(vehicleVo);
            //查询总记录数
            Integer count=vehicleService.getCount(vehicleVo);
            return pageResult.success(list,count);
        }else {
            //根据路线查询
            //查询所以数据
            List<AdminVehicleVo> list=vehicleService.getVehicleListByRoute(vehicleVo);
            //查询总记录数
            Integer count=vehicleService.getCountByRoute(vehicleVo);
            return pageResult.success(list,count);
        }
    }

    /**
     * 添加公交
     * @param vehicle
     * @return
     */
    @PostMapping("addVehicle")
    @ResponseBody
    public Result addVehicle(Vehicle vehicle){
        boolean save = vehicleService.save(vehicle);
        if (save) {
            return Result.success("新增成功");
        }
       return Result.fail("新增失败");
    }

    /**
     * 根据id获取公交信息
     * @param id
     * @return
     */
    @GetMapping("/getVehicle/{id}")
    public String getVehicleById(@PathVariable Long id, Model model){
        //获取公交信息
        Vehicle vehicle=vehicleService.getVehicleById(id);
        //获取路线信息
        List<Route> routeList = routeService.getRouteList();
        //获取分离类信息
        List<Type> typeList = typeService.getTypeList();
        model.addAttribute("vehicle",vehicle);
        model.addAttribute("routeList",routeList);
        model.addAttribute("typeList",typeList);
        return "admin/vehicleEdit";
    }

    /**
     * 修改信息
     * @param vehicle
     * @return
     */
    @PostMapping("updateVehicle")
    @ResponseBody
    public Result updateVehicle(Vehicle vehicle){
        Boolean flag=vehicleService.updateVehicle(vehicle);
        if (flag) {
            return Result.success("公交信息修改成功！");
        }
        return Result.fail("公交信息修改失败！");
    }

    /**
     * 删除公交信息
     * @param ids
     * @return
     */
    @PostMapping("/{ids}")
    @ResponseBody
    public Result deleteVehicleByIds(@PathVariable("ids") String ids){
        vehicleService.deleteVehicleByIds(ids);
        return Result.success("删除成功！");
    }

    /**
     * 判断公交名是否可用
     * @param name
     * @return
     */
    @GetMapping("/judgeAvailable")
    @ResponseBody
    public Result judgeAvailable(String name){
        if ("".equals(name)){
            return Result.success("");
        }
        Vehicle vehicle=vehicleService.getVehicleByName(name);
        if (vehicle == null) {
            return Result.success("分类名可用");
        }
        return Result.fail("分类名已存在不可用");
    }
}

